//
//  RegisterViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14/10/20.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//

#import "RegisterViewController.h"
#import "Toast+UIView.h"
#import "NSString+SBJSON.h"
#import "ASIFormDataRequest.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)btnBack:(id)sender {
    [self  dismissViewControllerAnimated:YES completion:^{}];
}

- (IBAction)btnGetSMSCode:(id)sender {
    //请求url
    NSURL *url = [NSURL URLWithString:p_getsmscodeURL];
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
    NSString *str = [NSString stringWithFormat:@"&phone=%@",self.txtPhone.text];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *responseStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [responseStr JSONValue];
    [self.view makeToast:jsonDict[@"errmsg"]
                duration:1.0
                position:@"bottom"];
}

- (IBAction)btnReg:(id)sender {
    //请求url
    NSURL *url = [NSURL URLWithString:registerURL];
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
    NSString *str = [NSString stringWithFormat:@"&phone=%@&password=%@&car_number=%@&code=%@&platform=2",self.txtPhone.text,self.txtPassword.text,self.txtCarNum.text,self.txtSMSCode.text];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *responseStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [responseStr JSONValue];
    
    int errnum = [jsonDict[@"errno"] intValue];
    if(errnum == 0) {
        self.userBl = [[UserBl alloc] init];
        self.user = [[User alloc] init];
        self.user.id = 1;
        self.user.carNumber = self.txtCarNum.text;
        self.user.phone = self.txtPhone.text;
        self.user.password = self.txtPassword.text;
        self.user.token = jsonDict[@"token"];
        [self.userBl add:self.user];
        [self dismissViewControllerAnimated:YES completion:^{NSLog(@"注册成功！");}];
    } else {
        [self.view makeToast:jsonDict[@"errmsg"]
                    duration:1.0
                    position:@"bottom"];
    }
    
}

@end
